package com.yituo.mjworld.mvc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yituo.mjworld.core.web.core.BaseForm;
import com.yituo.mjworld.core.web.core.Result;
import com.yituo.mjworld.mvc.entity.other.AccountChangeLog;
import com.yituo.mjworld.mvc.entity.other.UserAccount;
import com.yituo.mjworld.mvc.entity.web.TransactionLog;
import com.yituo.mjworld.mvc.entity.web.UserRole;
import com.yituo.mjworld.mvc.entity.web.WebUser;
import com.yituo.mjworld.mvc.form.DeleteForm;
import com.yituo.mjworld.mvc.form.QueryForm;
import com.yituo.mjworld.mvc.service.BaseService;
import org.junit.jupiter.api.Test;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 赏金计划的主要模型
 */
@Service
public class AccountServerImpl extends BaseService {

  public static void main(String[] args) {
    AccountServerImpl server = new AccountServerImpl();
  }

  /**
   * 支付记录.
   * <p>
   * 每条用户的支付成功记录都会调用抽成计划来计算每一级用户的
   *
   * @param paylog
   */
  public void giftMoney(TransactionLog paylog) {
    // 消费者/下单者
    WebUser buyUser = (WebUser) baseDao.get(WebUser.class, paylog.getUserId());
    // 该消费者的上级代理用户
    WebUser parentUser = (WebUser) baseDao.get(WebUser.class, buyUser.getReferrer());
//    System.out.println("订单金额:" + paylog.getAmount() + "(消费者支付订单后的金额)");
    // 消费者实际支付到公司账户的钱.
    BigDecimal amount = paylog.getAmount();
    // 每一级应该拿到的钱.初始化为0.
    BigDecimal sumMoney = BigDecimal.ZERO;
    int i = 0;
    // 最深循环不过6层.
    while (parentUser != null && i <= 6) {
      // 查询上级代理的账户
      UserAccount parentAccount = (UserAccount) baseDao.get(UserAccount.class, parentUser.getId());
      if (parentAccount == null) {
        // 没有对应记录则创建
        parentAccount = new UserAccount();
        parentAccount.setUserId(parentUser.getId());
        parentAccount.setRealMoney(BigDecimal.ZERO);
        parentAccount.setRestMoney(BigDecimal.ZERO);
      }
//      System.out.println("当前用户货款状况:" + JSONObject.toJSONString(parentAccount));
      // 算出上级代理需要在前端显示的货款,以及统计他的实际利润是.
      if (UserRole.isAgent(parentUser)) {
        // 上级代理的浮动价格点数
        BigDecimal scale = BigDecimal.valueOf(parentUser.getScale() / 100.00);
        BigDecimal parentPrice = amount.divide(scale.add(BigDecimal.ONE), 2, BigDecimal.ROUND_DOWN);
        // 当前用户的实际利润
        BigDecimal realMoney = amount.subtract(parentPrice);
        // 上级代理的余额加上实际的利润
        sumMoney = sumMoney.add(realMoney);
        amount = amount.subtract(realMoney);
        // 统计用户实际利润
        parentAccount.setRealMoney(parentAccount.getRealMoney().add(realMoney));
        // 统计用户可提现货款(还有部分要打给下级代理)
        parentAccount.setRestMoney(parentAccount.getRestMoney().add(sumMoney));
        // 记录变动钱的数量

        AccountChangeLog log = new AccountChangeLog();
        log.setId(0);
        log.setChangeMoney(sumMoney);
        log.setBeforeMoney(parentAccount.getRestMoney().subtract(sumMoney));
        log.setAfterMoney(parentAccount.getRealMoney());
        log.setChangeType(1);
        log.setChangeDesc("抽成");
        log.setChangeDate(new Date());
        log.setUserId(parentUser.getId());
        baseDao.save(log);

      }
      baseDao.save(parentAccount);
      // 继续取出上级代理
      parentUser = (WebUser) baseDao.get(WebUser.class, parentUser.getReferrer());
    }
  }


  /**
   * @return
   */
  public BigDecimal getParentPrice() {
    return null;
  }

  @Test
  public void test() {
    BigDecimal i = BigDecimal.valueOf(0.01);
    BigDecimal i2 = BigDecimal.valueOf(0.01);
    // 向下取整模式
    System.out.println(i.multiply(i2).setScale(2, BigDecimal.ROUND_DOWN));
  }

  @Override
  public JSONObject getList(QueryForm form) {
    return null;
  }

  @Override
  public Result delete(DeleteForm form) {
    return null;
  }

  @Override
  public Result saveOrUpdate(BaseForm baseForm) throws InvocationTargetException, IllegalAccessException {
    return null;
  }
}
